package org.zhzh.crawler.pageprocessor;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zhzh.crawler.model.BTHeavenModel;
import org.zhzh.crawler.service.BTHeavenService;

import com.google.common.collect.Lists;
import com.google.gson.Gson;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

@Service
public class BTHeavenPageProcessor implements PageProcessor {

	private static Logger log = Logger.getLogger(BTHeavenPageProcessor.class);

	private static ThreadLocal<SimpleDateFormat> sdf = new ThreadLocal<SimpleDateFormat>() {// 时间处理器
		public SimpleDateFormat initialValue() {
			return new SimpleDateFormat("yyyy");
		}
	};
	private static ThreadLocal<SimpleDateFormat> sdfymd = new ThreadLocal<SimpleDateFormat>() {// 时间处理器
		public SimpleDateFormat initialValue() {
			return new SimpleDateFormat("yyyy/MM/dd");
		}
	};

	@Autowired
	private BTHeavenService btHeavenService;

	public void process(Page page) {
		if (page.getUrl().regex("PageNo=").match()) {
			processList(page);
		} else {
			processDetail(page);
		}
	}

	/**
	 * 列表页逻辑
	 */
	private void processList(Page page) {
		boolean stop = true;// 爬虫收敛
		// 详情页
		List<Selectable> subjectSTs = page.getHtml().xpath("//div[@class=ml]/div[@class=item]").nodes();
		int i = 0;
		for (Selectable subjectST : subjectSTs) {
			try {
				String uniqueName = subjectST.xpath("//p[@class=tt]/a/b/font/text()").get();// 名称（取法1）
				if (StringUtils.isBlank(uniqueName)) {
					uniqueName = subjectST.xpath("//p[@class=tt]/a/b/text()").get();// 名称（取法2）
				}
				if (StringUtils.isBlank(uniqueName)) {
					continue;
				}
				if (btHeavenService.isExist(uniqueName)) {
					continue;
				}
				stop = false;
				String createTimeStr = subjectST.xpath("//p[@class=tt]/span/text()").get();// 时间
				String link = subjectST.xpath("//p[@class=tt]").links().regex("/subject/.*").get();// 链接
				if (StringUtils.isBlank(createTimeStr) || StringUtils.isBlank(link)) {
					continue;
				}
				long createTime = sdfymd.get().parse(createTimeStr).getTime();
				page.addTargetRequests(Lists.newArrayList(link), createTime);// 按时间优先级
				i++;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		log.error(page.getUrl() + "----->subject:" + i);
		if (stop) {
			// page.setSkip(true);
			return;
		}
		// 列表页
		List<Selectable> pageSTs = page.getHtml().xpath("//ul[@class=pagelist]/li").nodes();// 分页列表
		boolean thisclass = false;// 当前页标识
		for (Selectable pageST : pageSTs) {
			if (thisclass) {
				String nextLink = pageST.links().get();// 当前页的下一页
				page.addTargetRequest(nextLink);
				log.error(page.getUrl() + "----->next:" + nextLink);
			}
			thisclass = pageST.xpath("//li[@class=thisclass]").match();
		}
	}

	/**
	 * 详情页逻辑
	 */
	private void processDetail(Page page) {
		try {
			BTHeavenModel model = new BTHeavenModel();
			// 时间
			long createTime = page.getRequest().getPriority();
			if (createTime < 1000l) {
				String createTimeStr = page.getHtml().xpath("//ul[@class=moviedteail_list]/li[4]/a/text()").get();
				if (StringUtils.isNumeric(createTimeStr)) {
					createTime = sdf.get().parse(createTimeStr).getTime();
				}
			}
			// 译名
			String name = page.getHtml().xpath("//div[@class=moviedteail_tt]/h1/text()").get();
			// 索引名
			String uniqueName = page.getHtml().xpath("//div[@class=title]/h2/text()").get();
			// 原名
			String originalName = page.getHtml().xpath("//div[@class=moviedteail_tt]/span/text()").get();
			// 又名
			List<Selectable> otherNameSTs = page.getHtml().xpath("//ul[@class=moviedteail_list]/li[1]/a").nodes();
			StringBuilder otherNameSB = new StringBuilder();
			for (Selectable otherNameST : otherNameSTs) {
				otherNameSB.append(otherNameST.xpath("//a/text()").get()).append(",");
			}
			// 地区
			String position = page.getHtml().xpath("//ul[@class=moviedteail_list]/li[3]/a/text()").get();
			// 标签
			List<Selectable> tagSTs = page.getHtml().xpath("//ul[@class=moviedteail_list]/li[2]/a").nodes();
			StringBuilder tagSB = new StringBuilder();
			for (Selectable tagST : tagSTs) {
				tagSB.append(tagST.xpath("//a/text()").get()).append(",");
			}
			// 缩略图
			String thumbnail = page.getHtml().xpath("//div[@class=moviedteail_img]/a/img/@src").get();
			// 下载链接
			List<Selectable> urlSelectables = page.getHtml().$("div.sl.cl div.tinfo").nodes();
			List<String> urls = new ArrayList<String>();
			for (Selectable urlSelectable : urlSelectables) {
				urls.add(urlSelectable.xpath("//a/@href").get());
			}

			model.setCreateTime(createTime);// 时间
			model.setName(name);// 中文名
			model.setUniqueName(uniqueName);
			model.setOriginalName(originalName);// 原名
			model.setOtherNames(otherNameSB.toString());// 又名
			model.setPosition(position);// 地区
			model.setTags(tagSB.toString());// 标签
			model.setThumbnail(thumbnail);// 缩略图
			// model.setImages(images);// 图片
			Gson gson = new Gson();
			model.setUrls(gson.toJson(urls));// 下载链接
			page.putField("model", model);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Site getSite() {
		String userAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36";
		return Site.me().setRetryTimes(3).setCycleRetryTimes(3).setSleepTime(0).setCharset("utf-8").setUserAgent(userAgent);
	}

}
